const app = Vue.createApp({
    data() {
        return {
            dialogVisible: false,
            people: 0,
            isGroup: false,
            groups: [],
            number: 0,
            span: 0,
            formLabelWidth: '180px',
            offset: 0
        }
    },
    methods: {
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then((_) => {
                    done()
                })
                .catch((_) => {
                })
        },
        confirm() {
            this.dialogVisible = false
            this.isGroup = true
            this.getGroup()
        },
        rand(num) {
            let numbers = []
            for (let i = 1; i <= num; i++) {
                let rNum = Math.ceil(Math.random() * num)
                if (numbers.indexOf(rNum) == -1) {
                    numbers.push(rNum)
                } else {
                    i--
                }
            }
            return numbers
        },
        getType() {
            let rNum = Math.ceil(Math.random() * 4)
            switch (rNum) {
                case 1:
                    return 'success'
                    break;
                case 2:
                    return 'info'
                    break;
                case 3:
                    return 'warning'
                    break;
                case 4:
                    return 'danger'
                    break;
            }
        },
        getGroup() {
            // let numbers = this.rand(this.people)
            const people = [
                "秦爽", "苏文博", "杨龙", "金发魁", "倪国超",
                "谢文强", "朱伟东", "宋克源", "徐静", "孙培善",
                "雷玉倩", "牛国瑞", "张凯", "李晓明", "金文涛",
                "叶辉", "王福昌", "曹阳", "腾嘉巍", "张泽",
                "高云", "王亮", "郭子宇", "郭秉谦", "王雲慧",
                "王槐瑞", "孙吉翔", "李瑶瑶", "赵乐乐", "樊慧慧",
                "王博", "袁敏", "何鑫", "万君", "周浩",
                "赵文轩", "王鹤彬", "包鲜兵", "史伟鹏", "张欣艺",
                "张兴成", "王旭", "王斌斌", "田振鹏", "魏涛",
                "张艺健", "王静", "韩月", "姬丽丽", "李文杰",
                "李国威", "王乐", "陈海龙", "张亿多", "王程",
                "樊志斌", "周莹", "罗子涵", "王丽丽", "杨博涵",
                "白馨悦", "王雪", "田宝臣", "蒋万喜", "魏春锡",
                "苏炳智", "牛元博", "杨玉萍", "聂瑾宸", "彭浩润",
                "张奎", "朱义平", "邹宇明", "马炎", "朱红兵",
                "赵荣兴", "魏倩文", "赵文翔", "张崇杰", "韩玉斌",
                "刘成伟", "何雨铭"
            ];
            this.shuffleArray(people)
            const groupSize = 5;
            // const randomGroups = this.createGroups(people, groupSize);
            let group = [];
            console.log(people.length)
            for (let i = 1; i <= people.length; i++) {
                group.push({
                    type: this.getType(),
                    number: people[i - 1]
                })
                if (i % groupSize === 0) {
                    this.groups.push({
                        type: this.getType(),
                        numbers: group,
                    })
                    group = []
                }
            }
            if (group) {
                for (let i = 0; i < group.length; i++) {
                    this.groups[this.groups.length - i - 1 > 0 ? this.groups.length - i - 1 : 0].numbers
                        .push(group[i])
                }
            }
            console.log(this.groups)
            window.sessionStorage.setItem("groups", JSON.stringify(this.groups))
            this.span = Math.floor(24 / this.groups.length) < 2 ? 2 : Math.floor(24 / this.groups
                .length)
            this.offset = Math.floor((24 - this.span * this.groups.length) / 2)
        },

        // 打乱数组顺序
        shuffleArray(array) {
            for (let i = array.length - 1; i > 0; i--) {
                const j = Math.floor(Math.random() * (i + 1));
                [array[i], array[j]] = [array[j], array[i]];
            }
        },
        // 随机分组，每组 5 个人
        createGroups(people, groupSize) {
            const shuffledPeople = [...people]; // 复制数组
            this.shuffleArray(shuffledPeople); // 打乱数组顺序

            const groups = [];
            while (shuffledPeople.length > 0) {
                groups.push(shuffledPeople.splice(0, groupSize));
            }
            return groups;
        }
    },
    mounted() {
        const groups = JSON.parse(window.sessionStorage.getItem("groups"))
        console.log(groups);
        if (groups) {
            this.isGroup = true
            this.groups = groups
            this.span = Math.floor(24 / this.groups.length) < 2 ? 2 : Math.floor(24 / this.groups
                .length)
            this.offset = Math.floor((24 - this.span * this.groups.length) / 2)
        }
    }
})
app.use(ElementPlus);
app.mount("#view")